package com.label.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.label.entity.*;
import com.label.mapper.LabelbindMapper;
import com.label.mapper.LabelbindvoMapper;
import com.label.service.LabelbindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.label.service.LabelsService;
import com.label.service.LogService;
import com.label.service.PatientlabelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jianhua
 * @since 2022-03-27
 */
@Service
public class LabelbindServiceImpl extends ServiceImpl<LabelbindMapper, labelbind> implements LabelbindService {
    @Autowired
    private LabelbindMapper labelbindMapper;
    @Autowired
    LogService logService;
    @Autowired
    LabelsService labelsService;
    @Autowired
    PatientlabelsService patientlabelsService;
    @Autowired
    HttpSession session;
    @Autowired
    private LabelbindvoMapper labelbindvoMapper;
    @Override
    public Page pageList(Page page, Integer id){
        if (page==null){
            page =new Page();
        }
        QueryWrapper wrapper=new QueryWrapper();
        if (id!=null){
            wrapper.eq("patientid",id);
        }
        return super.page(page,wrapper);
    }

    @Override
    public labelbindvo getById(Integer id){
        QueryWrapper<labelbindvo> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("limitid",id);
        queryWrapper.eq("limitstate",1);
        labelbindvo labelbindvo=labelbindvoMapper.selectOne(queryWrapper);
        return labelbindvo;
    }
    @Override
    public labelbind getbindById(Integer id){
        labelbind labelbind=labelbindMapper.getlabelsById(id);
        return labelbind;
    }
    @Override
    public boolean saveOrUpdate(labelbindvo labelbindvo){
        int i= labelbindvoMapper.insert(labelbindvo);
        if (i>0){
            //        日志记录
            Log log=new Log();
            log.setUsername((String) session.getAttribute("username"));
            log.setLabelid(labelbindvo.getLabelid());
            Labels labels=labelsService.getById(labelbindvo.getLabelid());
            log.setLabelname(labels.getLabelname());
            log.setOperationType("绑定标签");
            log.setOperationtime(LocalDateTime.now());
            log.setPatientid(labelbindvo.getPatientid());
            Patientlabels patientlabels=patientlabelsService.getById(labelbindvo.getPatientid());
            log.setPatientname(patientlabels.getPatientname());
            System.out.println(log);
            logService.logsave(log);
            return true;
        }else return false;
    }
    @Override
    public boolean update(labelbindvo labelbindvo){
        int i= labelbindvoMapper.updateById(labelbindvo);
        if (i>0){
            //        日志记录
            Log log=new Log();
            log.setUsername((String) session.getAttribute("username"));
            log.setLabelid(labelbindvo.getLabelid());
            Labels labels=labelsService.getById(labelbindvo.getLabelid());
            log.setLabelname(labels.getLabelname());
            log.setOperationType("修改标签绑定信息");
            log.setOperationtime(LocalDateTime.now());
            log.setPatientid(labelbindvo.getPatientid());
            Patientlabels patientlabels=patientlabelsService.getById(labelbindvo.getPatientid());
            log.setPatientname(patientlabels.getPatientname());
            System.out.println(log);
            logService.logsave(log);
            return true;
        }else return false;
    }
    @Override
    public boolean updateById(labelbindvo labelbindvo){
        int i= labelbindvoMapper.updateById(labelbindvo);
        if (i>0){
            //        日志记录
            Log log=new Log();
            log.setUsername((String) session.getAttribute("username"));
            log.setLabelid(labelbindvo.getLabelid());
            Labels labels=labelsService.getById(labelbindvo.getLabelid());
            log.setLabelname(labels.getLabelname());
            log.setOperationType("解绑标签");
            log.setOperationtime(LocalDateTime.now());
            log.setPatientid(labelbindvo.getPatientid());
            Patientlabels patientlabels=patientlabelsService.getById(labelbindvo.getPatientid());
            log.setPatientname(patientlabels.getPatientname());
            System.out.println(log);
            logService.logsave(log);
            return true;
        }else return false;
    }
    @Override
    public List<labelbind> selectList(Integer id){
        QueryWrapper<labelbind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patientid", id);
        List labels= labelbindMapper.selectList(queryWrapper);
        System.out.println(labels);
        return labels;
    }
}
